from rest_framework import serializers
from goods.models import SPU, Brand, GoodsCategory


class SPUSerializer(serializers.ModelSerializer):
    """ 商品SPU 模型类序列化器 """

    # 关联的品牌，名称
    brand = serializers.StringRelatedField(read_only=True)
    brand_id = serializers.IntegerField()
    # 需求要求返回 category1_id 而不是 category1
    category1_id = serializers.IntegerField()
    category2_id = serializers.IntegerField()
    category3_id = serializers.IntegerField()

    class Meta:
        model = SPU
        exclude = ["create_time", "update_time", "category1", "category2", "category3"]


class BrandSerializer(serializers.ModelSerializer):
    """ 品牌 模型类序列化器 """

    class Meta:
        model = Brand
        fields = ["id", "name"]


class GoodsCategorySerializer(serializers.ModelSerializer):
    """ 商品类别 模型类序列化器 """

    class Meta:
        model = GoodsCategory
        fields = ["id", "name"]

